<template>
  <div>
    <el-card shadow="hover" style="margin-bottom: 20px">
      <div class="flex-warp">
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-input placeholder="请输入学生姓名"
                      size="small"
                      suffix-icon="el-icon-search"
                      v-model="listQueryParam.name"
            >
            </el-input>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-input placeholder="请输入考试名称"
                      size="small"
                      suffix-icon="el-icon-search"
                      v-model="listQueryParam.examName"
            >
            </el-input>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-input placeholder="请输入题目名称"
                      size="small"
                      suffix-icon="el-icon-search"
                      v-model="listQueryParam.questionName"
            >
            </el-input>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button size="small"
                       icon="el-icon-search"
                       @click="search"
            >搜索
            </el-button>
          </div>
        </div>

        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button type="primary"
                       size="small"
                       icon="el-icon-refresh-right"
                       @click="refresh"
            >刷新
            </el-button>
          </div>
        </div>

        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button type="danger"
                       size="small"
                       icon="el-icon-folder-delete"
                       @click="batchDel"
            >删除
            </el-button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card shadow="hover"
             class="mt15"
    >
      <el-table :data="tableData"
                stripe
                ref="multipleTable"
                @selection-change="handleSelectionChange"
                style="width: 100%"
      >
        <el-table-column type="selection"
                         width="55"
        >
        </el-table-column>
        <el-table-column prop="name"
                         align="center"
                         label="学生姓名"
        >
        </el-table-column>
        <el-table-column prop="grade"
                         align="center"
                         label="学生分数"
        >
        </el-table-column>
        <el-table-column prop="questionName"
                         align="center"
                         label="题目名称"
        >
        </el-table-column>
        <el-table-column prop="questionGrade"
                         align="center"
                         label="题目分数"
        >
        </el-table-column>

        <el-table-column prop="examName"
                         align="center"
                         label="考试名称"
        >
        </el-table-column>

        <el-table-column prop="createTime"
                         align="center"
                         label="创建时间"
        >
        </el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
        >
          <template slot-scope="scope">
<!--            <el-button @click="updateClick(scope.row)"-->
<!--                       type="text"-->
<!--                       size="small"-->
<!--            >编辑-->
<!--            </el-button>-->
            <el-button type="danger"
                       size="small"
                       icon="el-icon-delete" 
                       @click="delClick(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0"
                  :total="total"
                  :page.sync="listQueryParam.current"
                  :limit.sync="listQueryParam.pageSize"
                  @pagination="getTableDataList"
      />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialogTitle"
               :visible.sync="addOrUpdateDialogVisible"
               width="40%"
               :before-close="handleClose"
    >
      <el-form :model="formData"
               :rules="rules"
               ref="formData"
               label-width="100px"
      >
        <el-form-item label="所属学生"
                      prop="studentId"
        >
          <el-select v-model="formData.studentId" clearable placeholder="请选择所属学生">
            <el-option
              v-for="item in studentIdOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属考试"
                      prop="examId"
        >
          <el-select v-model="formData.examId" clearable placeholder="请选择所属考试">
            <el-option
              v-for="item in examIdOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属试题"
                      prop="questionId"
        >
          <el-select v-model="formData.questionId" clearable placeholder="请选择所属试题">
            <el-option
              v-for="item in questionIdOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分数"
                      prop="grade"
        >
          <el-input-number v-model="formData.grade" :precision="2" :step="0.1" :max="1000000"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer"
      >
        <el-button @click="addOrUpdateDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitForm('formData')"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  insert,
  update,
  get,
  del,
  getStudentDate,
  getExamDate,
  getQuestionsDate
} from '@/api/ExamDetail/index'
import Pagination from '@/components/Pagination'
import { tableUtil } from '@/mixins/table'
import { Message } from 'element-ui'
import uploadImg from '@/components/ImageUpload'
import ImgPreview from '@/components/ImgPreview'

export default {
  mixins: [tableUtil],
  components: {
    Pagination, uploadImg, ImgPreview
  },
  data() {
    return {
      type: localStorage.getItem('examType'),
      listQueryParam: {
        current: 1,
        pageSize: 5
      },
      formData: {},
      rules: {
        studentId: [
          { required: true, message: '请选择所属学生', trigger: 'change' }
        ],
        examId: [
          { required: true, message: '请选择所属考试', trigger: 'change' }
        ],
        questionId: [
          { required: true, message: '请选择所属试题', trigger: 'change' }
        ],
        grade: [
          { required: true, message: '请输入分数', trigger: 'blur' }
        ],
        teacherId: [
          { required: true, message: '请输入所属老师', trigger: 'blur' }
        ]
      },
      studentIdOptions: [],
      examIdOptions: [],
      questionIdOptions: []
    }
  },
  mounted() {
    this.getTableDataList()
    this.getStudentDateList()
    this.getExamDateList()
    this.getQuestionsDateList()
  },
  methods: {
    getTableDataList() {
      if (this.type == 1) {
        this.listQueryParam.teacherId = localStorage.getItem('examId')
      }
      getList(this.listQueryParam).then((res) => {
        this.total = res.data.total
        this.tableData = res.data.records
      })
    },
    updateClick(row) {
      this.dialogTitle = '修改'
      get(row.id).then((res) => {
        this.formData = res.data
        this.addOrUpdateDialogVisible = true
      })
    },
    delByIds(ids) {
      del(ids).then((res) => {
        Message.success('删除成功')
        this.getTableDataList()
      })
    },
    saveData() {
      if (this.formData.id) {
        update(this.formData).then((res) => {
          Message.success('修改成功')
          this.addOrUpdateDialogVisible = false
          this.getTableDataList()
        })
      } else {
        insert(this.formData).then((res) => {
          Message.success('添加成功')
          this.addOrUpdateDialogVisible = false
          this.getTableDataList()
        })
      }
    },
    getStudentDateList() {
      getStudentDate().then(res => {
        this.studentIdOptions = res.data
      })
    },
    getExamDateList() {
      getExamDate().then(res => {
        this.examIdOptions = res.data
      })
    },
    getQuestionsDateList() {
      getQuestionsDate().then(res => {
        this.questionIdOptions = res.data
      })
    }
  }
}
</script>
